﻿define(
    function (require) {

        var GlobalData = require('./GlobalData');
        var Deferred = require('er/Deferred');
        var $ = require('jquery');
        var u = require('underscore');
        var etpl = require('etpl');

        require('tpl!common/tpl/common.tpl.html')
        require('tpl!common/tpl/form.tpl.html')

        require('../biz/dashboard/config');
        require('../biz/equipment/config');    
        require('../biz/experienceTicket/config');     
        require('../biz/mall/config');
        require('../biz/report/config');
        require('../biz/experience/config');
        require('../biz/ticketShop/config');
        require('../biz/equipmentShop/config');

        /**
         * 开始初始化系统常量和用户常量，此时已经获取用户的全部信息
         *
         * @ignore
         */
        function initializeUserAndSystem() {
            var data = GlobalData.getInstance();
            return Deferred.all(data.loadUser(), data.loadSystem());
        }

        /**
         * 跳转到首页，在用户未登录的情况下执行
         *
         * @ignore
         */
        function redirectToIndex() {
            var baseURL = 'index.html';
            location.href = baseURL + location.hash;
            // 在`Promise`中，抛出异常会使其进入失败状态，
            // 一般来说跳转了就不会有下面的代码执行，这里就是防止进入成功状态
            throw new Error('Failed to redirect to index');
        }

        /**
         * 开始系统执行
         *
         * @ignore
         */
        function start() {
            overrideControlDefaults();
            initializeUserAndSystem()
                .then(initializeApplication, redirectToIndex);
        }

        /**
         * 重写控件默认属性
         *
         * @private
         * @method common.Main#overrideControlDefaults
         * @return {er.meta.Promise}
         */
        function overrideControlDefaults() {
            var esui = require('esui');
            var OverrideDefaults = require('ub-ria/ui/extension/OverrideDefaults');
            var defaults = require('common/uiDefaults');
            esui.attachExtension('OverrideDefaults', {overrides: defaults});
        }

        function initializeApplication(user, system) {
            // 显示用户信息
            if (!user.role) {
                user.role = 'ADMIN';
            }
            $('#username').html(user['username'])
            $('#userInfo').show();

            require('er/Deferred').syncModeEnabled = true;
            require('er/config').indexURL = getIndexUrl(user.role);

            // rule
            require('esui/validator/MaxRule');
            require('esui/validator/MinRule');
            require('esui/validator/MaxLengthRule');
            require('esui/validator/PatternRule');
            require('esui/validator/RequiredRule');
            require('esui/validator/MaxRule');

            require('common/ajax').enable();
            require('er').start();
            require('common/erTrack');

            // 初始化用户权限
            var userAuth = {};
            userAuth[user.role] = true;
            require('er/permission').add(userAuth);

            // 左侧导航
            var Navigator = require('common/navigator');
            var navigatorItems = require('common/navigatorItems');
            var navigator = new Navigator('nav');
            navigator.customClass = 'ui-tab-item iconfont';
            navigator.setNavigatorItems(navigatorItems);
            navigator.init();
        }

        function getIndexUrl(role) {
            return {
                ADMIN: '/equipment/list',
                PARTNER: '/equipmentShop/list',
            }[role];
        }

        return {
            start: start
        };
    }
);
